<?php

class Core_Component_Cms_Controller_Backend_Content{
    
    public function __construct() {
        $GLOBALS[RC]->getHelper()->setLocation('{cms.cms_menu}', '{cms.content_menu}');
    }
    
    public function mappingAction(){
        return array(
            'create'=>'edit'
        );
    }
    
    public function indexAction($pageindex = 1, $pagesize = 20){
        $q = App::getModel('cms/content')->getQuery();
        $list = $q->pagination($pageindex, $pagesize)->fetch();
        $count = $q->fetchCount();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list' => $list, 'record_count' => $count));
    }
    
    public function editAction($id = NULL) {
        $model = App::getModel('cms/content');
        $categories = App::getModel('cms/category')
                        ->getQuery()
                        ->condition('v.website_id', App::getRunningWebsite()->getId())
                        ->fetch();       
        if (!is_null($id)) {
            $model->load($id);
            $GLOBALS[RC]->getHelper()->setLocation($model->getTitle());
        }else{
            $GLOBALS[RC]->getHelper()->setLocation('{cms.create_content}');
        }
        return $GLOBALS[RC]->getHelper()->modelFormResult($model,
                array('category_id'=>array('data'=>$categories->toArray(), 'labelKey'=>'name', 'valueKey'=>'id')),
                '*/*/edit');
    }    
    
    public function saveAction(Core_Component_Cms_Model_Content $model) {
        $model->merge()->validate()->save();
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');
    }    
    
    public function publishPostAction($ids){
        $update = App::getModel('cms/content')
                    ->setStatus(CONSTANT_STATUS_PUBLISHED)
                    ->getVersionUpdate();
        $update->condition('version_id', $ids, 'in')
               ->condition('status', CONSTANT_STATUS_PUBLISHED, 'neq')
               ->execute();
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');        
    }
    
    public function forbidPostAction($ids){
        $update = App::getModel('cms/content')
                    ->setStatus(CONSTANT_STATUS_FORBIDDEN)
                    ->getVersionUpdate();
        $update->condition('version_id', $ids, 'in')
               ->condition('status', CONSTANT_STATUS_FORBIDDEN, 'neq')
               ->execute();
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');         
    }
    
    public function deletePostAction($ids){
        $models = App::getModel('cms/content')
                    ->getQuery()
                    ->condition('v.version_id', $ids, 'in')
                    ->fetchCollection();
        foreach($models as $model){
            $model->delete();
        }
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->redirectResult('*/*/*/index');       
    }    
}